<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=9" />
		<meta http-equiv="pragma" content="no-cache" />
		<title>文章类目树</title>
		<%@ include file="../../../common/Admin-list.jsp"%>
		
		<link rel="stylesheet" type="text/css" href="<%=bpath %>Scripts/ztree/zTreeStyle.css" />
		<link rel="stylesheet" type="text/css" href="<%=bpath %>Scripts/ztree/right_menu.css" />				
		<script type="text/javascript" src="<%=bpath %>Scripts/ztree/jquery.ztree.all-3.5.js"></script>
	</head>
<body>
<div class="easyui-panel" fit="true" type="panel" border="false">
	<div class="easyui-layout" data-options="fit:true,border:false" type="layout">
		<div data-options="region:'west',split:true,border:false" style="width:230px;">
			<div class="treelist module">
				<div class="hd">
					<h2 class="t">文章类目树</h2>
					<span class="handle4tree" title="显示/隐藏树列表"></span>					
				</div>
				<div class="bd">
					<div id="valleyTree" class="ztree"></div>
				</div>
			</div>
			<!-- 右键菜单div --> 		
			<div id="rMenu">  				 
				 <ul>
				 	<c:if test="${data.type==0}">				 	
			            <li id="m_add" onclick="addArticle();">
			            	<img align="middle" style="vertical-align:middle;"  src="<%=basePath%>/Scripts/easyui/themes/icons/edit_add.png"/>
			            	<span> 新增文章</span>
			            </li>
			            <li id="m_edit" onclick="searchArticle();">
		            		<img align="middle" style="vertical-align:middle;"  src="<%=basePath%>/Scripts/easyui/themes/icons/search.png"/>
		            		搜索文章
		            	</li>	
		            </c:if>
		           	<c:if test="${data.type==1}">			            
			            <li id="m_edit" onclick="searchArticle();">
		            		<img align="middle" style="vertical-align:middle;"  src="<%=basePath%>/Scripts/easyui/themes/icons/sum.png"/>
		            		统计栏目
		            	</li>	
		            </c:if>           
		        </ul>
			</div> 
		</div>
		
		<div data-options="region:'center',split:true,border:false">
			<iframe id="configPage" name="configPage" src=" " frameborder="0" width="100%" height="100%" allowTransparency></iframe>
		</div>
		<div id="win"></div>
	</div>
</div>
<script type="text/javascript">
	//加载左侧树
	var setting = {
		async : {
			enable : true,
			url : '../ArticleCategory/getzTreeNodes.json?noAdmin=yes',
			autoParam : [ 'id=parentID', 'level' ],//异步加载时需要自动提交父节点属性的参数
			//otherParam: { 'type' : 'area','site_type':'AA01'}, //区域 空气站 'site_type':'AA01,WM01,WM02'
			dataFilter : zTreefilter
		},
		check: {
			enable: true,
			autoCheckTrigger: true,
			chkboxType: { "Y": "ps", "N": "ps" }
		},		
		view : {
			//addHoverDom: addHoverDom,//当用户鼠标移动到节点上时,显示用户自定义控件,显示隐藏状态同zTree内部的编辑，删除按钮  
	      //  removeHoverDom: removeHoverDom//移出节点时,显示隐藏状态同zTree内部的编辑、删除按钮
		},
		callback : {
			//onDblClick: zTreeOnDbClick//双击节点			
			onRightClick : zTreeOnRightClick,//鼠标右键--新增文章，查询文章
			onCheck: zTreeOnCheck ,//选中的时候 
			onClick: zTreeOnClick,	
			onAsyncSuccess: onAsyncSuccess
			
		}
	};
		
	function zTreefilter(treeId, parentNode, childNodes) {
		if (!childNodes)
			return null;
		else {
			for (var i = 0, l = childNodes.length; i < l; i++) {
				if (childNodes[i].isParent=="false") { //不是父类
					childNodes[i].icon="<%=bpath %>Scripts/ztree/img/diy/2.png";				
				}else{
					//treeNode.children.length
					childNodes[i].halfCheck = true;					
				}
			}			
			return childNodes;
		}		
	}	
	
	//加载成功后 
	var firstAsyncSuccessFlag = 0;
	function onAsyncSuccess(event, treeId, treeNode, msg) {			
		var zTree = $.fn.zTree.getZTreeObj(treeId);		
		//加载 展开 子节点
		var node=zTree.getNodeByParam("isParent",true,null); //如有多个节点满足查询条件，只返回第一个匹配到的节点 
		zTree.expandNode(node,true,false,true);	
		
		if(treeNode.children.length!=0){
			if(treeNode.checked){ //如果选中 则展开全部子节点
				treeNode.halfCheck = false; // 取消 Halfcheck			
				var nodes = treeNode.children; //获得当前节点的子节点	
				for(var i = 0;i<nodes.length;i++){	//遍历每一个子节点更新其选中状态	
					zTree.checkNode(nodes[i],treeNode.checked,true,true);	
				}
			}
		}else{
			treeNode.checked=false;		
			zTree.setChkDisabled(treeNode, true); //没有站点 则禁用
		}		
	}
	
	var map = {}; //文章类目 id
	function zTreeOnCheck(event, treeId, treeNode) {		
		var treeObj = $.fn.zTree.getZTreeObj(treeId);		
		//展开当前节点 触发异步请求子节点的动作		
		treeObj.expandNode(treeNode,true,true,true);	
	  // alert(treeNode.id + ", " + treeNode.name + "," + treeNode.checked+","+treeNode.levelID);
		if(treeNode.levelID!="1"){
			if(treeNode.checked){		    	
		    	map[treeNode.id] = treeNode.name; //添加进入map site-code		    
		   	}else{ //取消选择
		    	if(!!map[treeNode.id]){
		    		delete map[treeNode.id];
		    	}		    	
		   	}
	        
		  	var i=0;var myArray=new Array(); var OrgIdArray=new Array();
			for(var prop in map){
			    if(map.hasOwnProperty(prop)){
			       myArray[i]=map[prop];
			       OrgIdArray[i]=prop; //map中key			       
			       i++;
			    }
			}		  
		    $(window.frames["configPage"].document).find("#categoryIds").val(OrgIdArray.join());
	  }	    	       
	    
	};		
	
	
	function zTreeOnClick(event, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj(treeId);
		if (treeNode.isParent) {
			zTree.expandNode(treeNode);
		} else {
			zTree.checkNode(treeNode,null,true,true);			
		}		
	}	
	
	//显示右键菜单  
    function showRMenu(type, x, y) {          
        if (type!="root") {  //根节点 不显示
        	 $("#rMenu ul").show(); 
        	 $("#rMenu").css({"top":y+"px", "left":x+"px", "display":"block"});  
        }          
    }  
    //隐藏右键菜单  
    function hideRMenu() {  
        $("#rMenu").hide();  
    }  
    
  //鼠标右键事件-创建右键菜单  
    function zTreeOnRightClick(event, treeId, treeNode) {  
    	var zTree = $.fn.zTree.getZTreeObj(treeId);    	
    	//alert(treeNode.id + ", " + treeNode.name + "," + treeNode.checked+","+treeNode.levelID);    	
        if (!treeNode) {  //不是节点        	
            zTree.cancelSelectedNode();  
            showRMenu("root", event.clientX, event.clientY);  
        } else if (treeNode && !treeNode.noR) { //noR属性为true表示禁止右键菜单  
            if (treeNode.levelID=="1" || treeNode.isParent) { //根节点     or 父类            	
                zTree.cancelSelectedNode();  
                showRMenu("root", event.clientX, event.clientY);  
            } else {              	
                zTree.selectNode(treeNode);  //选择该节点，但不是checkin
                showRMenu("node", event.clientX, event.clientY);  
            }  
        }  
    }  
  
    function reloadTree() {  
        hideRMenu();  
        $.fn.zTree.init($('#valleyTree'), setting); 
    }  	
  
	//页面加载 
	$(function() {		
		var type=${data.type}; 		
		if(type=="0"){ //文章列表
			var value="../ArticleList/listInterface.htm";
			$("#configPage").attr("src",value);
		}else if(type=="1"){//统计分析
			var value="../ArticleList/chartArticleCy.htm";
			$("#configPage").attr("src",value);
		}		
		
		reloadTree();  		
		
		 $("body").bind(//鼠标点击事件不在节点上时隐藏右键菜单  
	                "mousedown",  
	                function(event) {  
	                    if (!(event.target.id == "rMenu" || $(event.target)  
	                            .parents("#rMenu").length > 0)) {  
	                        $("#rMenu").hide();  
	                    }  
	                });  
	});
	
	
	
	//新建文章
	function addArticle(){
		hideRMenu();
		var zTree=$.fn.zTree.getZTreeObj("valleyTree");
	    var nodes = zTree.getSelectedNodes();
	    if (nodes && nodes.length > 0) { 	    	
	    	//打开新增文章界面
	    	// $(window.frames["configPage"].document).find("#categoryIds").val(OrgIdArray.join());	    	
	    	document.getElementById('configPage').contentWindow.leftAddArticle(nodes[0].id,nodes[0].name); 
	    }
	}
	
	//搜索--打开右侧的搜索
	function searchArticle(){
		hideRMenu();
		var zTree=$.fn.zTree.getZTreeObj("valleyTree");
	    var nodes = zTree.getSelectedNodes();
	    if (nodes && nodes.length > 0) { 	    		    	
	    	zTree.checkNode(nodes[0], true, true,true);	    	
	    	document.getElementById('configPage').contentWindow.searchList(); 
	    }
	}
	
	
</script>
</body>
</html>